/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAL;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Random;

/**
 *
 * @author MrKoi
 */
public class Classshare {
    Connect c=new Connect();
    Connection con;
    public String MD5(String str)
    {
        String text=str;    
        try 
         {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(text.getBytes());
            BigInteger dis = new BigInteger(1, md5.digest());
            text = dis.toString(16);        
         }
        catch(NoSuchAlgorithmException e) 
        {
            return "false";
        }
        return text;
    }
public String CodeRandom()
    {
        String str=new  String("QAa0bcLdUK2eHfJgTP8XhiFj61DOklNm9nBoI5pGqYVrs3CtSuMZvwWx4yE7zR");
 	StringBuffer sb=new StringBuffer();
 	Random r = new Random();
 	int te=0;
 	for(int i=1;i<=8;i++){
 		te=r.nextInt(62);
 		sb.append(str.charAt(te));
 	}
        return sb.toString();
    }
    public String GetCarID(){
        con=c.getConnection();
        String id="";
        try
        {
            String sql ="SELECT top 1 * FROM Cars ORDER BY CarID DESC";
            PreparedStatement psm=con.prepareStatement(sql);
            ResultSet rs=psm.executeQuery();
            String carid="";
            while(rs.next())
            {
                carid=rs.getString("CarID");
            }
            int num=Integer.parseInt(carid.substring(1,5));
            if(num < 9)
            {
             id="C000"+(num+1);
            }
            if(num==9)
            {
                id="C00"+(num+1);
            }
            if(num >9 && num<99)
            {
                id="C00"+(num+1);
            }
            if(num==99)
            {
                id="C0"+(num+1);
            }
            if(num >99 && num <999)
            {
                 id="C0"+(num+1);
            }
            if(num==999)
            {
                id="C"+(num+1);
            }
            if(num>999)
            {
                id="C"+(num+1);
            }
        }catch (Exception ex){id="C0001";}
        return id;
    }
    public String GetTourID()
    {
        con=c.getConnection();
        String id="";
        try
        {
            String sql ="SELECT top 1 * FROM TourPackages ORDER BY TourID DESC";
            PreparedStatement psm=con.prepareStatement(sql);
            ResultSet rs=psm.executeQuery();
            String tourid="";
            while(rs.next())
            {
                tourid=rs.getString("TourID");
            }
            int num=Integer.parseInt(tourid.substring(1,5));
            if(num < 9)
            {
             id="T000"+(num+1);
            }
            if(num == 9)
            {
             id="T00"+(num+1);
            }
            if(num >9 && num<99)
            {
                id="T00"+(num+1);
            }
            if(num == 99)
            {
                id="T0"+(num+1);
            }
            if(num >99 && num <999)
            {
                 id="T0"+(num+1);
            }
            if(num==999)
            {
                id="T"+(num+1);
            }
            if(num>999)
            {
                id="T"+(num+1);
            }
        }catch (Exception ex){id="T0001";}
        return id;
    }
}
